package com.sheng.whyclass.work.acitivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.StringRequest;
import com.sheng.util.Constant;
import com.sheng.util.CustomToast;
import com.sheng.util.HttpProcessManager;
import com.sheng.util.VolleyManager;
import com.sheng.whyclass.R;
import com.sheng.whyclass.person.event.AddScoreEvent;

import de.greenrobot.event.EventBus;

public class UpdateFinishedActivity extends Activity implements View.OnClickListener {

    private TextView workTitle;
    private TextView workContent;
    private LinearLayout workBottom;
    private EditText addScoreInput;
    private Button submit_score;


    private String s_id;
    private String answer;
    private String topic;

    private String score;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_finished);
        EventBus.getDefault().register(this);
        perseIntent();
        initView();
    }

    private void initView() {
        workTitle = (TextView) findViewById(R.id.work_title);
        workContent = (TextView) findViewById(R.id.work_content);
        workBottom = (LinearLayout) findViewById(R.id.work_bottom);
        addScoreInput = (EditText) findViewById(R.id.add_score_input);
        submit_score = (Button) findViewById(R.id.submit_score);
        submit_score.setOnClickListener(this);
        workContent.setText(answer);

    }

    public static void startUpdateFinishedActivity(Context context, String id,String answer,String topic) {
        Intent intent = new Intent(context, UpdateFinishedActivity.class);
        Bundle b = new Bundle();
        b.putString(Constant.KEY_CONTENT_ID, id);
        b.putString(Constant.CONTENT_ANSWER, answer);
        b.putString(Constant.TOPIC, topic);

        intent.putExtras(b);
        context.startActivity(intent);
    }

    private void perseIntent() {
        Intent intent = getIntent();
        if (intent == null || intent.getExtras() == null) {
            return;
        }

        Bundle b = intent.getExtras();
        s_id = b.getString(Constant.KEY_CONTENT_ID);
        answer = b.getString(Constant.CONTENT_ANSWER);
        topic = b.getString(Constant.TOPIC);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.submit_score:
                score = addScoreInput.getText().toString();
                if(TextUtils.isEmpty(score.trim())){
                    CustomToast.showLongToast(getString(R.string.para_null));
                }else{
                    StringRequest request  = HttpProcessManager.getInstance().addStudentScore(Constant.HOST_ADD_STUDENT_SCORE, s_id,topic,score);
                    VolleyManager.getInstance(getApplicationContext()).addToRequestQueue(request);
                }
                break;
        }
    }

    public void onEventMainThread(AddScoreEvent event) {


        int code = event.code;
        if (code == Constant.CODE_FAIL) {
            CustomToast.showToast(getString(R.string.net_fail));
            return;
        } else {
            CustomToast.showToast(event.info);
            finish();
            return;
        }

    }
}
